using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using Stratman.Utilities;

namespace Stratman.Utilities.NetFlix
{
    /// <summary>
    /// Contains general properties and details for a movie on NetFlix's system.
    /// </summary>
    public class Movie
    {
        /// <summary>
        /// Numeric identifier for the movie on NetFlix's system.
        /// </summary>
        protected long id = 0;
        /// <summary>
        /// Title of the movie.
        /// </summary>
        protected string title = "";
        /// <summary>
        /// URL for the movie.
        /// </summary>
        protected string url = "";
        /// <summary>
        /// Details (rating, synopsis, actors, etc.) for the movie.
        /// </summary>
        protected MovieDetails details = null;
        /// <summary>
        /// Random number generator to help randomize the image URL properties and keep us from
        /// swamping one of NetFlix's image servers.
        /// </summary>
        protected Random randomNumber = new Random();

        /// <summary>
        /// Constructor that instantiates the ID and title properties.
        /// </summary>
        /// <param name="id">
        /// Numeric identifier for the movie on NetFlix's system.
        /// </param>
        /// <param name="title">
        /// Title of the movie.
        /// </param>
        public Movie(long id, string title)
        {
            this.id = id;
            this.title = title;

            string normalizedTitle = title;

            // Replace spaces and special characters with underscores to help get the movie's base
            // URL
            normalizedTitle = Regex.Replace(normalizedTitle, " \\(\\d-Disc Series\\)$", "");
            normalizedTitle = Regex.Replace(normalizedTitle, "([^a-zA-Z0-9\\-_])+", " ");
            normalizedTitle = normalizedTitle.Trim();
            normalizedTitle = normalizedTitle.Replace(' ', '_');

            url = "http://www.netflix.com/Movies/" + normalizedTitle + "/" + id.ToString();
        }

        /// <summary>
        /// Numeric identifier for the movie on NetFlix's system.
        /// </summary>
        public long ID
        {
            get
            {
                return id;
            }
        }

        /// <summary>
        /// Title of the movie.
        /// </summary>
        public string Title
        {
            get
            {
                return title;
            }
        }

        /// <summary>
        /// Base URL for the movie's information.
        /// </summary>
        public string Url
        {
            get
            {
                return url;
            }
        }

        /// <summary>
        /// URL to the thumbnail image of the movie's box art.
        /// </summary>
        public string SmallBoxImageUrl
        {
            get
            {
                return String.Format("http://cdn-{0}.nflximg.com/us/boxshots/small/{1}.jpg", randomNumber.Next(0, 9), id);
            }
        }

        /// <summary>
        /// URL to the normal-sized image of the movie's box art.
        /// </summary>
        public string LargeBoxImageUrl
        {
            get
            {
                return String.Format("http://cdn-{0}.nflximg.com/us/boxshots/large/{1}.jpg", randomNumber.Next(0, 9), id);
            }
        }

        /// <summary>
        /// Details (rating, synopsis, actors, etc.) for the movie.  This property utilizes the
        /// lazy load design pattern because we need to make a separate request to NetFlix's
        /// servers to retrieve this information.  The object is only instantiated once.
        /// </summary>
        public MovieDetails Details
        {
            get
            {
                if (details == null)
                {
                    JsonMovieDetails jsonDetails = (JsonMovieDetails)WebRequestUtilities.MakeJsonGetRequest("http://www.netflix.com/JSON/BobData?linkCtr=BOX&movieid=" + id.ToString(), typeof(JsonMovieDetails));
                    details = new MovieDetails(jsonDetails);
                }

                return details;
            }
        }
    }
}
